﻿-- Author      : Kurapica
-- Create Date : 6/12/2008 1:12:50 AM

-- FontInstance
do
	-- Check Version
	local version = 1
	if not IGAS:RegisterFile("IGAS.GUI.Widget.FontInstance", version) then
		return
	end

    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = CreateFrame
    
	local _WidgetName = "FontInstance"
    local _Base = "UIObject"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
    local _BaseFunc = IGAS.GUI.Base.Func
	local _PropertyType = IGAS.GUI.Base.PropertyType   
	
    -- FuncProxy
	local _FuncProxy = {
        --- Original Functions
        -- GetFont() - Return the font file, height, and flags. 
        ["GetFont"] = function(self)
            return self.__UI:GetFont()
        end,
        
        -- GetFontObject() - Return the 'parent' Font object, or nil if none. 
        ["GetFontObject"] = function(self)
            return self.__UI:GetFontObject()
        end,
        
        -- GetJustifyH() - Return the horizontal text justification. 
        ["GetJustifyH"] = function(self)
            return self.__UI:GetJustifyH()
        end,
        
        -- GetJustifyV() - Return thevertical text justification. 
        ["GetJustifyV"] = function(self)
            return self.__UI:GetJustifyV()
        end,
        
        -- GetShadowColor() - Returns the color of text shadow (r, g, b, a). 
        ["GetShadowColor"] = function(self)
            return self.__UI:GetShadowColor()
        end,
        
        -- GetShadowOffset() - Returns the text shadow offset (x, y). 
        ["GetShadowOffset"] = function(self)
            return self.__UI:GetShadowOffset()
        end,
        
        -- GetSpacing() - Returns the text spacing. 
        ["GetSpacing"] = function(self)
            return self.__UI:GetSpacing()
        end,
        
        -- GetTextColor() - Returns the default text color. 
        ["GetTextColor"] = function(self)
            return self.__UI:GetTextColor()
        end,
        
        -- SetFont("path", height[,"flags"]) - Sets the font to use for text, returns 1 if the path was valid, nil otherwise (no change occurs). 
        ["SetFont"] = function(self, path, height, flags)
            self.__UI:SetFont(path, height, flags)
        end,
        
        -- SetFontObject(fontObject) - Sets the 'parent' Font object from which this object inherits properties. 
        ["SetFontObject"] = function(self, fontObject)
            self.__UI:SetFontObject(fontObject)
        end,
        
        -- SetJustifyH("justifyH") - Sets horizontal text justification ("LEFT","RIGHT", or "CENTER") 
        ["SetJustifyH"] = function(self, justifyH)
            self.__UI:SetJustifyH(justifyH)
        end,
        
        -- SetJustifyV("justifyV") - Sets vertical text justification ("TOP","BOTTOM", or "MIDDLE") 
        ["SetJustifyV"] = function(self, justifyV)
            self.__UI:SetJustifyV(justifyV)
        end,
        
        -- SetShadowColor(r, g, b[, a]) - Sets the text shadow color. 
        ["SetShadowColor"] = function(self, r,g,b,a)
            self.__UI:SetShadowColor(r,g,b,a)
        end,
        
        -- SetShadowOffset(x, y) - Sets the text shadow offset. 
        ["SetShadowOffset"] = function(self, x,y)
            self.__UI:SetShadowOffset(x,y)
        end,
        
        -- SetSpacing(spacing) - Sets the spacing between lines of text in the object. 
        ["SetSpacing"] = function(self, spacing)
            self.__UI:SetSpacing(spacing)
        end,
        
        -- SetTextColor(r, g, b[, a]) - Sets the default text color. 
        ["SetTextColor"] = function(self, r,g,b,a)
            self.__UI:SetTextColor(r,g,b,a)
        end,
    }
    
	-- Property
	local _Property = {
		--- Font
		["Font"] = { 
			["Set"] = function(self, font)
				local flags = ""
				if font.outline then
					if font.outline == "NORMAL" then
						flags = flags.."OUTLINE"
					elseif font.outline == "THICK" then
						flags = flags.."THICKOUTLINE"
					end
				end
				if font.monochrome then
					if flags ~= "" then
						flags = flags..","
					end 
					flags = flags.."MONOCHROME"
				end

				self:SetFont(font.path, font.size, flags)
			end,

			["Get"] = function(self)
				local font = {}
				local flags
				font.path, font.size, flags = self:GetFont()
				flags = flags or ""
				if string.find(flags, "THICKOUTLINE") then
					font.outline = "THICK"
				elseif string.find(flags, "OUTLINE") then
					font.outline = "NORMAL"
				end
				if string.find(flags, "MONOCHROME") then
					font.monochrome = true 
				end 

				return font
			end,

			["Type"] = _PropertyType.Font,
		}, 
		--- FontObject
		["FontObject"] = { 
			["Set"] = function(self, fontObject)
				self:SetFontObject(fontObject)
			end,

			["Get"] = function(self)
				return self:GetFontObject()
			end,

			["Type"] = _PropertyType.FontObject,
		}, 
		--- JustifyH
		["JustifyH"] = { 
			["Set"] = function(self, justifyH)
				self:SetJustifyH(justifyH)
			end,

			["Get"] = function(self)
				return self:GetJustifyH()
			end,

			["Type"] = _PropertyType.JustifyH,
		}, 
		--- JustifyV
		["JustifyV"] = { 
			["Set"] = function(self, justifyV)
				self:SetJustifyV(justifyV)
			end,

			["Get"] = function(self)
				return self:GetJustifyV()
			end,

			["Type"] = _PropertyType.JustifyV,
		},
		--- ShadowColor
		["ShadowColor"] = { 
			["Set"] = function(self, color)
				self:SetShadowColor(color.red, color,green, color.blue, color.alpha)
			end,

			["Get"] = function(self)
				local color = {}
				color.red, color,green, color.blue, color.alpha = self:GetShadowColor()

				return color
			end,

			["Type"] = _PropertyType.Color,
		},
		--- ShadowOffset
		["ShadowOffset"] = { 
			["Set"] = function(self, offset)
				self:SetShadowOffset(offset.x, offset.y)
			end,

			["Get"] = function(self)
				local offset = {}
				offset.x, offset.y = self:GetShadowOffset()

				return offset
			end,

			["Type"] = _PropertyType.Offset,
		},
		--- Spacing
		["Spacing"] = { 
			["Set"] = function(self, spacing)
				self:SetSpacing(spacing)
			end,

			["Get"] = function(self)
				return self:GetSpacing()
			end,

			["Type"] = _PropertyType.Number,
		}, 
		--- TextColor
		["TextColor"] = { 
			["Set"] = function(self, color)
				self:SetTextColor(color.red, color,green, color.blue, color.alpha)
			end,

			["Get"] = function(self)
				local color = {}
				color.red, color,green, color.blue, color.alpha = self:GetTextColor()

				return color
			end,

			["Type"] = _PropertyType.Color,
		},
	}
	
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
    }
    
    _Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
end
